//
//  MainViewController.swift
//  imitationMicroblog
//
//  Created by 季晓侠 on 16/2/21.
//  Copyright © 2016年 季晓侠. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        addChildViewControllers()
        
        setTabbar()
        print(tabBar.subviews)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置按钮到顶部
        tabBar.bringSubviewToFront(composeButton)
        //设置按钮位置
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, width*2, 0)
    }
    //MARK 监听方法
    @objc private func clickComposeButton(){
    
       print("点击按钮")
    
    }
    //MARK 私有控件
    //懒加载
    
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        let imageName = "tabbar_compose_icon_add"
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        return button
    
    }()
}
//MARK: - 设置tabbar
extension MainViewController{

    private func setTabbar(){
        tabBar.shadowImage = UIImage.xxj_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named:"tabbar_background")
        
        tabBar.addSubview(composeButton)
    
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }

    

}
//MARK:  - 添加控制器
extension MainViewController{
  //添加所有的子控制器
    private func addChildViewControllers() {
      addChildViewController(HomeViewController(), title: "首页",imageName:"tabbar_home" )
      addChildViewController(MeassageViewController(),
        title: "消息",imageName:"tabbar_message_center" )
        
      addChildViewController(UIViewController())
        
      addChildViewController(DiscoverViewController(),
        title: "发现",imageName:"tabbar_discover" )
      addChildViewController(ProfileViewController(),
        title: "我的",imageName:"tabbar_profile" )
    }
    
    
    
    //设置控制器属性 加载到tabBarController
    private func addChildViewController(vc:UIViewController, title:String,imageName:String) {
        
        
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.image = UIImage(named:imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
       
        addChildViewController(nav)
       
    
    }
  
    }


